package com.jbit.hxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbit.hxy.common.Result;
import com.jbit.hxy.mapper.*;
import com.jbit.hxy.model.dto.CategoryFromDTO;
import com.jbit.hxy.model.dto.CategoryQueryDTO;
import com.jbit.hxy.model.po.*;
import com.jbit.hxy.model.vo.MenuVO;
import com.jbit.hxy.model.vo.PageVO;
import com.jbit.hxy.service.menu.CategoryDishService;
import com.jbit.hxy.service.menu.CategorySetmealService;
import com.jbit.hxy.service.menu.WxCategoryService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 28575
* @description 针对表【wx_category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2024-09-07 23:33:48
*/
@Service
@RequiredArgsConstructor
public class WxCategoryServiceImpl extends ServiceImpl<WxCategoryMapper, WxCategory>
    implements WxCategoryService {
    private final WxCategoryMapper wxCategoryMapper;
    private final CategoryDishMapper categoryDishMapper;
    private final CategorySetmealMapper categorySetmealMapper;
    private final WxDishMapper wxDishMapper;
    private final WxSetmealMapper wxSetmealMapper;

    @Override
    public Result getCategory(CategoryQueryDTO categoryQueryDTO) {
        //使用Lambda表达式【指定需要查询的字段】
        IPage<WxCategory> page = new Page<>(categoryQueryDTO.getCurPage(),
                categoryQueryDTO.getPageSize());
        //2.设置查询条件
        QueryWrapper<WxCategory> queryWrapper = new QueryWrapper<>();
        //用户姓名
        queryWrapper.lambda().like(StringUtils.isNotBlank(
                        categoryQueryDTO.getName()),
                WxCategory::getName,
                categoryQueryDTO.getName());

        //手机号
        queryWrapper.lambda().eq(categoryQueryDTO.getType() != null,
                WxCategory::getType,
                categoryQueryDTO.getType());


        //查询指定字段
        //queryWrapper.select("id", "title");
        //排除字段查询
//        queryWrapper.select(WxUser.class, info -> !info.getColumn().equals("deleted"));
        //3.查询
        IPage<WxCategory> pageResult = wxCategoryMapper.selectPage(page, queryWrapper);


        //4.封装，返回
        PageVO pageVo = new PageVO<>();

        pageVo.setTotal(pageResult.getTotal());
        pageVo.setList(pageResult.getRecords());

        pageVo.setPageSize(pageResult.getSize());
        pageVo.setCurrentPage(pageResult.getCurrent());


        return Result.success(pageVo);
    }

    @Override
    public Result addCategory(CategoryFromDTO categoryFromDTO) {
        WxCategory wxCategory = new WxCategory();
        BeanUtils.copyProperties(categoryFromDTO, wxCategory);
        wxCategory.setCreatetime(LocalDateTime.now());
        wxCategory.setUpdatetime(LocalDateTime.now());
        wxCategoryMapper.insert(wxCategory);
        return Result.success("添加成功");
    }

    @Override
    public Result updateCategory(CategoryFromDTO categoryFromDTO) {

        WxCategory wxCategory = new WxCategory();
        BeanUtils.copyProperties(categoryFromDTO, wxCategory);
        wxCategory.setUpdatetime(LocalDateTime.now());
        wxCategoryMapper.updateById(wxCategory);
        return Result.success("修改成功");
    }

    @Override
    public Result deleteCategory(Integer id) {
        wxCategoryMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result getCategorys() {
        List<WxCategory> wxCategories = wxCategoryMapper.selectList(null);
//        ArrayList<MenuVO> menuVOS = new ArrayList<>();
//        for (WxCategory wxCategory : wxCategories) {
//            MenuVO<Object> objectMenuVO = new MenuVO<>();
//            BeanUtils.copyProperties(wxCategory, objectMenuVO);
//            objectMenuVO.setId(wxCategory.getId());
//            if (wxCategory.getType() == 1){
//                List<Integer> dishIds = categoryDishMapper.selectByCateId(wxCategory.getId());
//                if (!dishIds.isEmpty()){
//                    ArrayList<WxDish> wxDishes = new ArrayList<>();
//                    for (Integer id : dishIds) {
//                        WxDish wxDish = wxDishMapper.selectById(id);
//                        if (wxDish != null){
//                            objectMenuVO.getChildren().add(wxDish);
//                        }
//
//                    }
//                }
//
//
//            } else {
//                List<Integer> setmealId = categorySetmealMapper.selectByCateId(wxCategory.getId());
//                if (!setmealId.isEmpty()) {
//                    ArrayList<WxSetmeal> wxSetmeals = new ArrayList<>();
//                    for (Integer id : setmealId) {
//                        WxSetmeal wxSetmeal = wxSetmealMapper.selectById(id);
//                        if (wxSetmeal != null) {
//                            objectMenuVO.getChildren().add(wxSetmeal);
//                        }
//
//                    }
//
//                }
//            }
//            menuVOS.add(objectMenuVO);
//        }
        return Result.success(wxCategories);
    }

    @Override
    public Result saveDishesByCateId(List<CategoryDish> list) {
        if (list.size() != 0) {
            Integer categoryId = list.get(0).getCategory_id();
            categoryDishMapper.deleteByCateId(categoryId);
            for (CategoryDish categoryDish : list) {
                categoryDishMapper.insert(categoryDish);
            }
        } else {

        }
        return Result.success("操作成功");

    }

    @Override
    public Result deleteAll(Integer cateId) {
        categoryDishMapper.deleteAllDishesByCateId(cateId);
        return Result.success("操作成功");
    }

}




